﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MYA_WPF.Model;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

namespace MYA_WPF.Utils
{
    class DataManager
    {
        
        public static void subscribeUser(MYA_WPF.Model.User user, MYA_WPF.Model.Contact contact, MYA_WPF.Model.Personne person) 
        {
            using (ManageYourAppointmentEntities dbcontext = new ManageYourAppointmentEntities())
            { 
                 dbcontext.Users.Add(user);
                 dbcontext.SaveChanges();

                 dbcontext.Contacts.Add(contact);
                 dbcontext.SaveChanges();

                 Contact contactContext = (from c in dbcontext.Contacts where (c.Email == contact.Email) select c).FirstOrDefault<Contact>();
                 person.Contact = contactContext;

                 User userContext = (from u in dbcontext.Users where (u.login == user.login) select u).FirstOrDefault<User>();
                 person.User = userContext;

                 dbcontext.Personnes.Add(person);
                 dbcontext.SaveChanges();

             }
        }

        public static ContactDetail getContactDetailByIdContact( int c)
        {

            ContactDetail contactDetail = null;
            using (ManageYourAppointmentEntities dbcontext = new ManageYourAppointmentEntities())
            {             
                var query =
                    from cc in dbcontext.Contacts
                    where cc.ContactID == c
                    select new { 
                        IdContact = cc.ContactID,
                        Nom = cc.Personnes.First<Personne>().Nom,
                        Prenom = cc.Personnes.First<Personne>().Prenom,
                        DateNaissance = cc.Personnes.First<Personne>().DateNaissance,
                        Email = cc.Email,
                        Telephone = cc.Telephone,
                             
                    };
                foreach (var obj in query)
                {
                             
                    contactDetail = new ContactDetail();
                    contactDetail.idContact = obj.IdContact;
                    contactDetail.Nom = obj.Nom;
                    contactDetail.Prenom = obj.Prenom;
                    contactDetail.DateNaissance = obj.DateNaissance;
                    contactDetail.Email = obj.Email;
                    contactDetail.Telephone = obj.Telephone;

                }
                                     
             }
            return contactDetail;           
        }

        public static ContactDetail getContactDetailByIdPersonne(int p)
        {

            ContactDetail contactDetail = null;
            using (ManageYourAppointmentEntities dbcontext = new ManageYourAppointmentEntities())
            {
                var query =
                    from pp in dbcontext.Personnes
                    where pp.id == p
                    select new
                    {
                        IdContact = pp.Contact.ContactID,
                        Nom = pp.Nom,
                        Prenom = pp.Prenom,
                        DateNaissance = pp.DateNaissance,
                        Email = pp.Contact.Email,
                        Telephone = pp.Contact.Telephone,
                        Adresse=pp.Contact.adresse,

                    };
                foreach (var obj in query)
                {
                    contactDetail = new ContactDetail();
                    contactDetail.idContact = obj.IdContact;
                    contactDetail.Nom = obj.Nom;
                    contactDetail.Prenom = obj.Prenom;
                    contactDetail.DateNaissance = obj.DateNaissance;
                    contactDetail.Email = obj.Email;
                    contactDetail.Telephone = obj.Telephone;
                    contactDetail.Adresse = obj.Adresse;
                
                }

            }
            return contactDetail;
        }

        public static void createEvent(Event pEvent, List<ContactDetail> pContactDetails, String pPathImage)
        {


            using (ManageYourAppointmentEntities dbcontext = new ManageYourAppointmentEntities())
            {

                List<Contact> contacts = new List<Model.Contact>();
                foreach (ContactDetail contactDetailX in pContactDetails)
                {
                    ContactDetail cd = contactDetailX;
                    Contact contact = getContactByID(cd);
                   // pEvent.Contacts.Add(contact);
                }
                //pEvent.Contacts = contacts;
                Image image = new Image();
                image.ImagePath = pPathImage;
                dbcontext.Images.Add(image);
                dbcontext.Events.Add(pEvent);
                dbcontext.SaveChanges();


                ImageEvent imageEvent = new ImageEvent();
                imageEvent.Event = pEvent;
                imageEvent.Image = image;
                imageEvent.isMainImage = true;
                dbcontext.ImageEvents.Add(imageEvent);
                dbcontext.SaveChanges();
                pEvent.ImageEvents.Add(imageEvent);
                image.ImageEvents.Add(imageEvent);
              



            }


        }

        public static Contact getContactByID(ContactDetail cd)
        {
            using (ManageYourAppointmentEntities dbcontext = new ManageYourAppointmentEntities())
            {
                Contact contact = (from c in dbcontext.Contacts where (c.ContactID == cd.idContact) select c).FirstOrDefault<Contact>();
                return contact;
            }
        }

    }
}
